package cn.felord.message.comsumer;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听器.
 *
 * @author tanqian
 * @version v1.0
 * @since 2019 /8/2
 */
@Component
public class Receiver {
    private static final Logger log= LoggerFactory.getLogger(Receiver.class);

    /**
     * DIRECT模式.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_DIRECT_QUEUE"})
    public void directQueue(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_DIRECT_QUEUE: "+new String (message.getBody()));
    }

    /**
     * FANOUT广播队列监听A
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_FANOUT_QUEUE_A"})
    public void fanoutQueueA(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_FANOUT_QUEUE_A: "+new String(message.getBody()));
    }

    /**
     * FANOUT广播队列监听B
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception   这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_FANOUT_QUEUE_B"})
    public void fanoutQueueB(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_FANOUT_QUEUE_B: "+new String(message.getBody()));
    }

    /**
     * TOPIC广播队列监听A
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_TOPIC_QUEUE_A"})
    public void topicQueueA(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_TOPIC_QUEUE_A: "+new String(message.getBody()));
    }

    /**
     * TOPIC广播队列监听B
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception   这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_TOPIC_QUEUE_B"})
    public void topicQueueB(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_TOPIC_QUEUE_B: "+new String(message.getBody()));
    }

    /**
     * HEADERS模式.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_HEADERS_QUEUE"})
    public void headersQueue(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("HAND_HEADERS_QUEUE: "+new String (message.getBody()));
    }

    /**
     * 监听替补队列 来验证死信.
     *
     * @param message the message
     * @param channel the channel
     * @throws IOException the io exception  这里异常需要处理
     */
    @RabbitListener(queues = {"HAND_REDIRECT_QUEUE"})
    public void redirectQueue(Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("dead message  10s 后 消费消息 {}",new String (message.getBody()));
    }
}
